<?php 

// The front-end needs to include the Exam View
// classes file, and the chosen back-end.
include 'classes.php';
include 'backend-oracle.php';

function build_table($recordset) {
	// This function is passed in an Exam View
	// recordset, and returns a HTML representation.

	if ( count($recordset) == 0 ) {
		// If we get here, there were no results in the
		// recordset. We simply return an error in a
		// paragraph tag.
		$return = "<p class=\"evProblem\">There are no results on record</p>";
	} else {
		// If we get here, there were results in the
		// recordset. We generate the table.
		
		$return = "";
		
		// The altRow variable is used to add a class
		// of 'altRow' to every second row. This lets
		// you do cool stuff like alternate the row
		// colours between white and light grey.
		// If you don't intend to use this, don't
		// worry. It won't do any harm and you don't
		// need to remove it.
		$altRow = true;
		
		$oldCourseTitle = "";
		
		// Now we output each row
		foreach($recordset as $record) {
			
			// Here, we actually add the altRow class
			// to every second row. A description of
			// altRow is above.
			
			if($oldCourseTitle != $record->courseTitle) {
				if ($oldCourseTitle != "") {
					$return .= "</tbody></table>";
				}

				$return .= "<h3>".$record->courseTitle."</h3>";
				$return .= "<table class=\"examResults\">";
				$return .= 	"<thead><tr>".
				   	"<th class=\"unitCode\">Unit Code</th>".
				   	"<th class=\"unitTitle\">Unit Title</th>".
				   	"<th class=\"year\">Year</th>".
				   	"<th class=\"grade\">Grade</th>".
				   	"</tr></thead><tbody>";
				$altRow = true;
			}
			
			$oldCourseTitle = $record->courseTitle;
			
			if ( $altRow )
				$return .= "<tr class=\"altRow\">";
			else
				$return .= "<tr>";
			$altRow = !$altRow;
			
			// Output each value that we want
			$return .= "<td>".$record->moduleCode."</td>";
			$return .= "<td>".$record->moduleTitle."</td>";
			$return .= "<td>".$record->period."</td>";
			$return .= "<td>".$record->result."</td>";
			$return .= "</tr>";
		}
		// Now we close the table.
		$return .= "</tbody></table>";
	}
	return($return);
}

function get_student_id($username) {
	
	// Replace this with whatever method you use
	// to turn your front-end system's usernames
	// into usernames your student record system
	// uses. In the example below, Glasgow Met's
	// VLE uses student's matriculation numbers
	// preceeded by 'met'. To get the user's
	// student ID from their Moodle username, all
	// we have to do is strip the 'met' off the 
	// beginning, if it exists.
	
	$contains = strpos($username, "met");

	if ($contains === 0) {
		return(substr($username, 3));
	} else {
		return($username);	
	}
	
	// a triple-equals is needed above, because
	// we need to check the type matches. Otherwise,
	// a value of 'false' (the username doesn't start
	// with 'met') would be mistaken for the value '0'
	// (indicating 'met' is at the start of the string).
}

?>